/**
* 项目名称：tv   
* 类名称：TVJsoupParser   
* 类描述：   
* 创建人：Issac   
* 创建时间：2011-7-10 下午03:22:37   
* 修改人：Issac   
* 修改时间：2011-7-10 下午03:22:37   
* 修改备注：   
* @version 
 */
package com.tv.htmlparser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.tv.bean.Program;
import com.tv.crawler.FileDownLoader;
import com.tv.data.service.ITVService;
import com.tv.tools.Configuration;

/**
 * @ClassName: TVJsoupParser
 * @Description: TODO
 * @author issac2009@vip.qq.com
 * @date 2011-7-10 下午03:22:37
 */
@Controller
public class TVJsoupParser {

	/*
	 * @Title: parseHtml
	 * @Description: 取网易电视节目单，id为datalist下面的数据
	 * @param @param args    
	 * @return void  
	 * @throws 
	 */
	public void parseHtml(String filename){
		//save path
		/*
		String fileStr = Class.class.getClass().getResource("/").getPath();
		Configuration rc = new Configuration(fileStr + "init.properties");
		String savepath = rc.getValue("website.src.file") + filename;
		String parsepath = rc.getValue("website.src.page") + filename;
		*/
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("init.properties");   
		Properties p = new Properties();   
		try {   
			   p.load(inputStream);   
		} 
		catch (IOException e1) {   
			   e1.printStackTrace();   
		}  
		String savepath = p.getProperty("website.src.file") + filename;
		String parsepath = p.getProperty("website.src.page") + filename;
		
		
		String data = "";
		FileDownLoader downLoader=new FileDownLoader();
		File parseFile = new File(parsepath);
		
		Document doc;
		try {
			doc = Jsoup.parse(parseFile, "gb2312");
		
			Element content = doc.getElementById("datalist");
			
			if (content == null || content.getElementsByTag("ul") == null){
				return; 
			}
			
			Elements ulElement = content.getElementsByTag("ul");
			
			for (Element ul : ulElement){
				Elements liElement = ul.getElementsByTag("li");
				
				for (Element li : liElement){
					Elements spanElement = li.getElementsByTag("span");
					int i = 0;
					
					for (Element span : spanElement){
						String til = span.text();
						data += til + "|";
						//System.out.print(data);
					}
					data += "\n";
					//System.out.println();
				}
			}
			downLoader.saveToLocal(data.getBytes(), savepath + ".txt");
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	/** 
	 * 遍历文件夹下的文件，并打印文件夹下文件的路径 
	 * @param directoryPath 文件夹目录 
	 */  
	public static List<String> traversal(String directoryPath) {  
		List<String> fileList = new ArrayList<String>();
	    File dir = new File(directoryPath);  
	    File[] files = dir.listFiles();  
	    if (files == null) {  
	        return null;  
	    } else {  
	        for (int i = 0; i < files.length; i++) {  
	            if (files[i].isDirectory()) {  
	                traversal(files[i].getAbsolutePath());  
	            } else {  
	            	fileList.add(files[i].getName());
	            }  
	        }  
	    }
	    return fileList;
	}  
	
	/**
	 * @Title: main
	 * @Description: TODO
	 * @param @param args    
	 * @return void  
	 * @throws
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationDemo.xml");
		context.getBean("startQuertz");
		
		/*
		String fileStr = Class.class.getClass().getResource("/").getPath();
		Configuration rc = new Configuration(fileStr + "init.properties");
		String path = rc.getValue("website.src.page");
		 
		List<String> filesList = traversal(path);
		TVJsoupParser parser = new TVJsoupParser();
		for (String file : filesList){
			parser.parseHtml(file);
		}
		*/
	}

}
